import json
from django.shortcuts import HttpResponse
from web.API import api_555movie1


# 返回文本库
def _yy_a(code, db):
    if code <= len(db) - 1:
        return db[code]
    else:
        return db[0]


def _yy_return_text(status, code: int):
    success_db = ['返回成功', '成功']
    error_db = ['返回失败', '不支持的请求方式', '参数未填写完整']
    content_type_db = ['text/plain', 'application/json']
    if status == "success":
        _yy_a(code, success_db)
    elif status == 'error':
        _yy_a(code, error_db)
    elif status == 'content_type':
        _yy_a(code, content_type_db)


# 参数验证
def _yy_params_validate(data):
    if data is not None or data != "":
        return True
    else:
        return False, f"{data}是空"


# 返回首页数据
def index(request):
    return HttpResponse(json.dumps(api_555movie1.IndexAll()), status=200)


# 返回影视详细数据
def info(request):
    if request.method == 'GET':
        movie_url = request.GET
        if movie_url:
            return HttpResponse(json.dumps(api_555movie1.GetMovieInfo(movie_url=request.GET['movie_url'])), status=200,
                                content_type=_yy_return_text('content_type', code=1))
        else:
            return HttpResponse(_yy_return_text('error', 2), status=501)
    else:
        return HttpResponse("不支持该请求方式", status=500, content_type=_yy_return_text('content_type', code=0))


# 获取影视的播放信息、m3u8链接等
def play(request):
    if request.method == 'GET':
        movie_url = request.GET.get('movie_url')
        if movie_url:
            return HttpResponse(json.dumps(api_555movie1.GetMoviePlayInfo(movie_url=movie_url)), status=200,
                                content_type=_yy_return_text('content_type', 1))
        else:
            return HttpResponse(_yy_return_text(status='error', code=2), status=501)
    else:
        return HttpResponse(status=500, content_type=_yy_return_text(status='error', code=1))


# 获取影视类型更多
def more(request):
    if request.method == 'GET':
        movie_type = request.GET.get("movie_type")
        page = request.GET.get("page")
        if _yy_params_validate(movie_type) and _yy_params_validate(page):
            return HttpResponse(json.dumps(api_555movie1.GetMovieMore(movie_type=int(movie_type), page=int(page))), status=200,
                                content_type=_yy_return_text('content_type', 1))
        else:
            return HttpResponse(_yy_return_text('error', 2), status=501,
                                content_type=_yy_return_text(status='content_type', code=0))

    else:
        return HttpResponse(_yy_return_text(status='error', code=1), status=500,
                            content_type=_yy_return_text('content_type', 0))


# 搜索影视：根据片名搜索、根据人物名字搜索
def SesrchMovie(request):
    if request.method == 'GET':
        wd = request.GET.get("wd")
        movie_type = request.GET.get("movie_type")
        if _yy_params_validate(wd) and _yy_params_validate(movie_type):
            return HttpResponse(json.dumps(api_555movie1.SesrchMovie(movie_type=int(movie_type),wd=wd)), status=200,
                                content_type=_yy_return_text('content_type', 1))
        else:
            return HttpResponse(_yy_return_text('error', 2), status=501,
                                content_type=_yy_return_text(status='content_type', code=0))

    else:
        return HttpResponse(_yy_return_text(status='error', code=1), status=500,
                            content_type=_yy_return_text('content_type', 0))
